﻿Imports AForge.Imaging
Imports System.Drawing


Public Class FormDitherPicker

    Public Event filterSelected(ByVal sender As Object, ByVal args As EventArgs)

    Public bayer As New AForge.Imaging.Filters.BayerDithering()
    Public burkes As New AForge.Imaging.Filters.BurkesDithering()
    Public floyd As New AForge.Imaging.Filters.FloydSteinbergDithering()
    Public jarvis As New AForge.Imaging.Filters.JarvisJudiceNinkeDithering()
    Public ordered As New AForge.Imaging.Filters.OrderedDithering()
    Public sierra As New AForge.Imaging.Filters.SierraDithering()
    Public stucki As New AForge.Imaging.Filters.StuckiDithering()
    Public thresh As New AForge.Imaging.Filters.Threshold()

    Public filter As AForge.Imaging.Filters.IFilter

    Public gs As AForge.Imaging.Filters.Grayscale = Nothing
    'Public bitmap As Bitmap = Nothing
    'Public smallBitmap As Bitmap = Nothing
    Public smallRGBBitmap As Bitmap = Nothing
    Public smallGrey32bppRGBBitmap As Bitmap = Nothing

    Public filename As String = ""

    Public weightedR As Decimal = 0.33
    Public weightedG As Decimal = 0.33
    Public weightedB As Decimal = 0.33

    Public imageDitherer As ImageDitherer = Nothing

    Public Sub setImageToChangeDither(ByVal imageDitherer As ImageDitherer)
        Me.imageDitherer = imageDitherer
        trackbarR.Value = 50
        trackbarG.Value = 50
        trackbarB.Value = 50
        setGreyscaleImageRGB() '(0.33, 0.33, 0.33)
        updateGUI()
    End Sub

    Public Sub setGreyscaleImage(ByVal r As Decimal, ByVal g As Decimal, ByVal b As Decimal)
        gs = New AForge.Imaging.Filters.Grayscale(r, g, b) '(0.05, 0.9, 0.05) (0.2125, 0.7154, 0.0721)
        smallGrey32bppRGBBitmap = gs.Apply(imageDitherer.displayBitmapThumb)
    End Sub

    Public Sub calcWeightedRGB()

        Dim r = trackbarR.Value / 100
        Dim g = trackbarG.Value / 100
        Dim b = trackbarB.Value / 100
        Dim RGBTotal As Decimal = r + g + b
        If RGBTotal <> 0 Then
            weightedR = r / RGBTotal
            weightedG = g / RGBTotal
            weightedB = b / RGBTotal
        End If
    End Sub

    Public Sub setGreyscaleImageRGB()
        calcWeightedRGB()
        Debug.Print(weightedR & " - " & weightedG & " - " & weightedB)
        setGreyscaleImage(weightedR, weightedG, weightedB)
    End Sub

    Public Sub updateGUI()
        pictureboxInput.Image = imageDitherer.standardisedBitmapThumb
        pictureboxO1.Image = thresh.Apply(smallGrey32bppRGBBitmap)
        pictureboxO2.Image = bayer.Apply(smallGrey32bppRGBBitmap)
        pictureboxO3.Image = burkes.Apply(smallGrey32bppRGBBitmap)
        pictureboxO4.Image = floyd.Apply(smallGrey32bppRGBBitmap)
        pictureboxO5.Image = jarvis.Apply(smallGrey32bppRGBBitmap)
        pictureboxO6.Image = ordered.Apply(smallGrey32bppRGBBitmap)
        pictureboxO7.Image = sierra.Apply(smallGrey32bppRGBBitmap)
        pictureboxO8.Image = stucki.Apply(smallGrey32bppRGBBitmap)
    End Sub

    Public Function ThumbnailCallback() As Boolean
        Return False
    End Function

    Public Function getThumb(ByVal b As Bitmap, ByVal w As Integer, ByVal h As Integer) As System.Drawing.Image
        Dim myCallback As New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
        Return b.GetThumbnailImage(w, h, myCallback, IntPtr.Zero)
    End Function

    Private Sub trackbarR_Scroll(sender As Object, e As EventArgs) Handles trackbarR.Scroll
        setGreyscaleImageRGB()
        updateGUI()
    End Sub

    Private Sub trackbarB_Scroll(sender As Object, e As EventArgs) Handles trackbarG.Scroll
        setGreyscaleImageRGB()
        updateGUI()
    End Sub

    Private Sub trackbarG_Scroll(sender As Object, e As EventArgs) Handles trackbarB.Scroll
        setGreyscaleImageRGB()
        updateGUI()
    End Sub

    Public Sub raiseSelectionEvent(ByVal ditherObject As AForge.Imaging.Filters.IFilter, ByVal ditherName As String)
        calcWeightedRGB()
        imageDitherer.setDither(New SelectedSettings(ditherObject, ditherName, weightedR, weightedG, weightedB))
        RaiseEvent filterSelected(Me, New EventArgs())
        Me.Close()
    End Sub

    Private Sub pictureboxO1_Click(sender As Object, e As EventArgs) Handles pictureboxO1.Click
        raiseSelectionEvent(thresh, "Threshold")
    End Sub

    Private Sub pictureboxO2_Click(sender As Object, e As EventArgs) Handles pictureboxO2.Click
        raiseSelectionEvent(bayer, "Bayer")
    End Sub

    Private Sub pictureboxO3_Click(sender As Object, e As EventArgs) Handles pictureboxO3.Click
        raiseSelectionEvent(burkes, "Burkes")
    End Sub

    Private Sub pictureboxO4_Click(sender As Object, e As EventArgs) Handles pictureboxO4.Click
        raiseSelectionEvent(floyd, "Floyd")
    End Sub

    Private Sub pictureboxO5_Click(sender As Object, e As EventArgs) Handles pictureboxO5.Click
        raiseSelectionEvent(jarvis, "Jarvis")
    End Sub

    Private Sub pictureboxO6_Click(sender As Object, e As EventArgs) Handles pictureboxO6.Click
        raiseSelectionEvent(ordered, "Ordered")
    End Sub

    Private Sub pictureboxO7_Click(sender As Object, e As EventArgs) Handles pictureboxO7.Click
        raiseSelectionEvent(sierra, "Sierra")
    End Sub

    Private Sub pictureboxO8_Click(sender As Object, e As EventArgs) Handles pictureboxO8.Click
        raiseSelectionEvent(stucki, "Stucki")
    End Sub

End Class
